﻿using Exceptionless.Models.Data;
using ExpressionBuilder.Generics;
using ExpressionBuilder.Helpers;
using LDM.Admin.Models.ExpressionBuilderModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Utility.Webs.Clients;

namespace LDM.Admin.Controllers.Orm
{
    [AllowAnonymous]
    public class ExpressionBuilderController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取表数据
        /// </summary>
        /// <returns></returns>
        public IActionResult GetTables()
        {
            var obj = new[]
            {
               new{ Id = 1,  text ="Product" },
               new{ Id = 2,  text ="ProductColor" },
            };
            return Ok(obj);
        }

        /// <summary>
        /// 根据表名获取属性信息
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult GetTablePropertystring(GetTablePropertystringRequest request)
        {
            var reslut = new DBEntityType().GetTypePropertyByTableName(request.TableName);
            return Json(reslut);
        }

        /// <summary>
        /// 根据表名获取属性信息
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="propertyName">属性名称</param>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult GetTablePropertyInfos(GetTablePropertyInfoRequest request)
        {
            var propertyInfo = new DBEntityType().GetTypePropertyInfo(request.TableName);
            var propertyFirst = propertyInfo.FirstOrDefault(x => x.PropertyName == request.PropertyName);
            if (propertyFirst != null)
            {
                var supportedOperations = new OperationHelper()
                                .SupportedOperations(propertyFirst.PropertyType)
                                .Select(o => new
                                {
                                    Id = o.ToString(),
                                    Name = o.Name
                                })
                                .ToList();
                return Json(supportedOperations);
            }
            return Json(null);
        }
        #region 构建基础数据
        private static List<Product> Products = new()
        {
            Product.CreateObject(1,"实验室标样",new DateTime(2021,12,29,22,43,49),new DateTime (2021,12,29,23,43,51)),
            Product.CreateObject(2,"品样标管",new DateTime(2021,12,29,22,44,17),new DateTime (2021,12,29,22,44,19)),
            Product.CreateObject(4,"产品4",new DateTime(2022,1,21,21,50,52),new DateTime (2022,1,21,21,50,52)),
            Product.CreateObject(5,"产品5",new DateTime(2023,8,25,12,16,19),new DateTime (2023,8,25,12,16,19)),
        };
        private static List<ProductColor> ProductColors = new()
        {
            ProductColor.CreateObject(9,"测试色号2",2,2,"色号2的描述",new DateTime(2021,12,29,22,43,49),new DateTime(2021,12,29,22,43,49)),
              ProductColor.CreateObject(10,"测试色号1",2,1,"色号1的描述",new DateTime(2022,1,18,22,43,49),new DateTime(2022,1,18,22,43,49)),
                ProductColor.CreateObject(12,"测试色号4",2,4,"色号4的描述",new DateTime(2023,8,25,22,43,49),new DateTime(2023,8,25,22,43,49)),
                  ProductColor.CreateObject(13,"测试色号5",2,5,"色号5的描述",new DateTime(2023,8,25,22,43,49),new DateTime(2023, 8, 25,22,43,49)),
        };
        public List<ProductView> ProductList()
        {
            //using var db = DBContext.CreateContext();
            var product = Products.AsQueryable();
            var productColor = ProductColors.AsQueryable();
            return (from p in product
                    join pc in productColor on p.ID equals pc.Productld
                    select new ProductView
                    {
                        ID = p.ID,
                        ProductName = p.ProductName,
                        ColorName = pc.ColorName,
                        ColorDesc = pc.ColorDesc,
                        CreateTime = p.CreateTime,
                    }).ToList();
        }
        private OperationHelper operationHelper = new OperationHelper();
        #endregion

        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost, HttpGet]
        public IActionResult GetProductView(ExpressionBuilderRequest request)
        {
            var product = Products.AsQueryable();
            var productColor = ProductColors.AsQueryable();
            FifterQuery(request, ref product, ref productColor);
            var result = (from p in product
                          join pc in productColor on p.ID equals pc.Productld
                          select new ProductView
                          {
                              ID = p.ID,
                              ProductName = p.ProductName,
                              ColorName = pc.ColorName,
                              ColorDesc = pc.ColorDesc,
                              CreateTime = p.CreateTime,
                          });
            return Json(result);
        }

        private void FifterQuery(ExpressionBuilderRequest requests, ref IQueryable<Product> product, ref IQueryable<ProductColor> productColor)
        {
            foreach (var request in requests.ExpressionBuilders)
            {
                if (request != null &&
                                   !string.IsNullOrEmpty(request.TableName) &&
                                    !string.IsNullOrEmpty(request.Condition) &&
                                    !string.IsNullOrEmpty(request.PropertyName) &&
                                    !string.IsNullOrEmpty(request.Value1))
                {
                    if (request.TableName == "Product")
                    {
                        Filter<Product> filter = new Filter<Product>();
                        var value1 = new DBEntityType().GetValue(request.TableName, request.PropertyName, request.Value1);
                        filter.By(request.PropertyName, operationHelper.GetOperationByName(request.Condition), value1);
                        product = product.Where(filter);
                    }
                    if (request.TableName == "ProductColor")
                    {
                        Filter<ProductColor> filter = new Filter<ProductColor>();
                        var value1 = new DBEntityType().GetValue(request.TableName, request.PropertyName, request.Value1);
                        filter.By(request.PropertyName, operationHelper.GetOperationByName(request.Condition), value1);
                        productColor = productColor.Where(filter);
                    }
                }
            }
        }
    }
}
